import http from '../../requrt/http'
// pages/xigoods/xqgoods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id: '',
    goods: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.data.goods_id = options
  },
  async getdata() {
    let res = await http('goods/detail?goods_id=' + this.data.goods_id.goods_id)
    console.log(res.data.message);
    this.setData({
      goods: res.data.message
    })
  },
  jrshopgoods() {
    let cart = wx.getStorageSync('cart') || []
    let index = cart.findIndex(v => v.goods_id === this.data.goods.goods_id)
    if (index === -1) {
      this.data.goods.num = 1
      this.data.goods.checked = true
      cart.push(this.data.goods)
    } else {
      cart[index].num++
    }
    wx.setStorageSync('cart', cart)
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      // 防止用户手抖
      mask: true,
    })
  },
  heardergoods(e) {
    console.log(e);
    const urls = this.data.goods.pics.map(v => v.pics_mid)
    const carrect = e.currentTarget.dataset.url
    wx.previewImage({
      urls: urls,
      current: carrect,
      showmenu: true,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getdata()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})